package com.software.chemistry.controller;

import com.software.chemistry.constant.APiConstants;
import com.software.chemistry.domain.Result;
import com.software.chemistry.entity.StudentInfo;
import com.software.chemistry.entity.UserQuery;
import com.software.chemistry.service.StudentInfoService;
import com.software.chemistry.util.ImageFileUtils;
import com.software.chemistry.util.ResultUtil;
import com.software.chemistry.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/student_info")
public class StudentInfoController {
    @Autowired
    private StudentInfoService studentInfoService ;

    //根据id查询用户
    @GetMapping("/findStudentInfoByUserId/{userId}")
    private StudentInfo findStudentInfoByUserId(@PathVariable String userId){
        StudentInfo find = studentInfoService.findStudentInfoByUserId(userId) ;
        return find;
    }

    //修改个人信息
    @PostMapping("/updateStudentInfo")
    public StudentInfo updateStudentInfo(@RequestParam("userId") String userId,
                                         @RequestParam("userName") String userName,
                                         @RequestParam("nickName") String nickName,
                                         @RequestParam("realName") String realName,
                                         @RequestParam("gender") String gender,
                                         @RequestParam("school") String school,
                                         @RequestParam("classId") String classId,
                                         @RequestParam("studentId") String studentId){

        System.out.println(userId);
        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setUserId(userId);
        studentInfo.setUserName(userName);
        studentInfo.setNickName(nickName);
        studentInfo.setRealName(realName);
        studentInfo.setGender(gender);
        studentInfo.setSchool(school);
        studentInfo.setClassId(classId);
        studentInfo.setStudentId(studentId);

        StudentInfo find = studentInfoService.findStudentInfoByUserId(userId);
        studentInfo.setPassword(find.getPassword());

        StudentInfo update = studentInfoService.updateStudentInfo(studentInfo);

        return update;
    }

    //新增用户
    @RequestMapping(value = "/createStudentInfo",method = RequestMethod.POST )
    private Result<StudentInfo> createStudentInfo(@RequestParam("param_username") String param_username, @RequestParam("param_password") String param_password , StudentInfo studentInfo){
        studentInfo.setPassword(param_password);
        studentInfo.setUserName(param_username);
        studentInfo.setUserId("S" + UUID.randomUUID().toString());

        studentInfoService.createStudentInfo(studentInfo);
        return ResultUtil.success(studentInfo);
    }




    @PostMapping("/checkStudentInfoByUserName")
    private Result<StudentInfo> findStudentInfoByUserName(@RequestParam("param_username") String param_username,StudentInfo studentInfo){
        //判断用户名是否存在
        if(studentInfoService.findStudentInfoByUserName(param_username).size()>0){
            return ResultUtil.error("1","该用户名已存在",studentInfo);
        }
        return ResultUtil.success(studentInfo);
    }
    //验证用户名密码是否正确
    @PostMapping("/findStudentInfoByUserName")
    private Result<StudentInfo> findStudentInfoByUserName(@RequestParam("param_username") String param_username, @RequestParam("param_password") String param_password){

        List<StudentInfo> studentInfo=studentInfoService.verifyLoginUser(param_username,param_password);
        if(studentInfo.size()>0){
            return ResultUtil.success(studentInfo);
        }else
            return ResultUtil.error("2","用户名密码不匹配",studentInfo);
    }
    //查询联系人
    //根据用户名或者用户id
    @GetMapping("/findStudentInfosByUserIdOrUserName/{str}")
    private Result<UserQuery> findStudentInfosByuserIdOruserName(@PathVariable String str){
        List<UserQuery> userQueries = studentInfoService.findStudentInfosByUserIdOrUserName(str, str) ;
        return ResultUtil.success(userQueries);
    }
    @GetMapping("/findStudentInfos")
    private Result<UserQuery> findStudentInfos(){
        List<UserQuery> userQueries = studentInfoService.findStudentInfos() ;
        if (userQueries.size()> 0)
        {return ResultUtil.success(userQueries) ;}else {
            return ResultUtil.error("2", "没找到",userQueries);
        }
    }

    //按照班级访问联系人
    @GetMapping("/findStudentInfosByClassId/{ClassId}")
    private Result<UserQuery> findStudentInfosByClassId(@PathVariable String ClassId){
        List<UserQuery> userQueries = studentInfoService.findStudentInfosByClassId(ClassId);
        return ResultUtil.success(userQueries);
    }
}
